# -*- coding:gbk -*-
import api
import osuskill
from PIL import Image, ImageDraw, ImageFont


colorgray = (85, 85, 85, 255)
colorblue = (68, 170, 221, 255)
colorpink = (187, 17, 119, 255)

player = input('Enter your id(blank=quit): ')
while player:
    try:
        print('Grabbing data from Osu! and Osu!Skill...')
        soup = osuskill.getosuskilldata(player)
        skill = osuskill.getskill(soup)
        title = osuskill.getranktitle(soup)
        udata = api.rawuserdata(player)
        print('Drawing profile picture')
        rawpp = 'pp :' + udata[0]["pp_raw"] + \
            ' ( #' + udata[0]["pp_rank"] + ' )'
        raw2 = 'playcount :' + udata[0]["playcount"]
        raw3 = 'level : ' + str(round(float(udata[0]["level"]), 1))
        im = Image.open("bg.jpg")
        api.dluserava(player)
        ava = Image.open("ava.jpg")
        Exo_Light = ImageFont.truetype('Exo-Light.ttf', 25)
        Exo_Regular = ImageFont.truetype('Exo-Medium.ttf', 15)
        Exo_SemiBold = ImageFont.truetype('Exo-SemiBold.ttf', 50)
        d = ImageDraw.Draw(im)
        d.text((10, 120), player, font=Exo_Light, fill=colorgray)
        d.text((150, 20), rawpp, font=Exo_Regular, fill=colorgray)
        d.text((150, 40), raw2, font=Exo_Regular, fill=colorgray)
        d.text((150, 60), raw3, font=Exo_Regular, fill=colorgray)
        ava = ava.resize((90, 90))
        im.paste(ava, (15, 25))

        centerx = 360
        centery = 80
        r = 50
        sqrt23 = pow(3, 0.5) * 0.5

        def raito(skilllevel):
            return (pow(skilllevel, 0.5) / pow(1000, 0.5)) * 50

        stm = (centerx - raito(skill[0]) * sqrt23,
               centery + raito(skill[0]) * 0.5)
        ten = (centerx, centery + raito(skill[1]))
        agi = (centerx, centery - raito(skill[2]))
        acc = (centerx + raito(skill[3]) * sqrt23,
               centery + raito(skill[3]) * 0.5)
        pcs = (centerx + raito(skill[4]) * sqrt23,
               centery - raito(skill[4]) * 0.5)
        rac = (centerx - raito(skill[5]) * sqrt23,
               centery - raito(skill[5]) * 0.5)
        cost = 1
        for i in range(0, 3):
            cost = cost * (skill[i] / 100)
        for i in range(4, 6):
            cost = cost * (pow(skill[i], 0.3) / pow(100, 0.3))
        cost = cost * float(udata[0]["pp_raw"]) * float(udata[0]["pp_raw"]) / 1000000
        cost = pow(cost, 0.7) / 200
        cost=round(cost, 1)

        def rare(cost):
            if cost <= 0.5:
                return 'N'
            if cost > 0.5 and cost <= 1.2:
                return 'N+'
            if cost > 1.2 and cost <= 2.5:
                return 'R'
            if cost > 2.5 and cost <= 5.5:
                return 'R+'
            if cost > 5.5 and cost <= 9:
                return 'SR'
            if cost > 9 and cost <= 15:
                return 'SSR'
            if cost > 15 and cost <= 20:
                return 'UR'
            if cost > 20:
                return 'UR+'

        d.text((150, 80), 'Rare : ' + rare(cost),
               font=Exo_Regular, fill=colorgray)
        i=0
        for t in title:
            d.text((150, 100 + 20 * i), 'Title' + str(i + 1) +
                   ' : ' + t, font=Exo_Regular, fill=colorgray)
            i=i + 1
        d.text((centerx - 35, centery - 30), str(cost),
               font=Exo_SemiBold, fill=(30, 30, 30, 255))
        d.line((agi, rac), fill=colorpink, width=1)
        d.line((agi, pcs), fill=colorpink, width=1)
        d.line((ten, stm), fill=colorpink, width=2)
        d.line((ten, acc), fill=colorpink, width=2)
        d.line((pcs, acc), fill=colorpink, width=2)
        d.line((stm, rac), fill=colorpink, width=2)

        im.show()
        im.save(player + '.jpg')
        print('Finished!')
        player=input('Enter your id(blank=quit): ')
    except:
        print('No id or NetWork error! Try again?')
        player=input('Enter your id(blank=quit): ')
    pass
